package com.pingf.estimation.service.impl;

import com.base.common.persistent.dao.EmployeeMapper;
import com.base.common.persistent.dao.EvaluatedLevelMapper;
import com.base.common.persistent.dao.EvaluatedPointMapper;
import com.base.common.persistent.dao.EvaluatedRecordMapper;
import com.base.common.persistent.model.Employee;
import com.base.common.persistent.model.EvaluatedLevel;
import com.base.common.persistent.model.EvaluatedPoint;
import com.base.common.persistent.model.EvaluatedRecord;
import com.base.common.persistent.query.EvaluatedLevelQuery;
import com.base.common.persistent.query.EvaluatedPointQuery;
import com.pingf.estimation.service.EstimationService;
import org.apache.ibatis.annotations.Case;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Kevin on 2017-6-10.
 */

@Service("estimationService")
public class EstimationServiceImpl implements EstimationService{

    @Resource
    private EvaluatedLevelMapper evaluatedLevelMapper;

    @Resource
    private EvaluatedPointMapper evaluatedPointMapper;

    @Resource
    private EvaluatedRecordMapper evaluatedRecordMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public List<EvaluatedLevel> queryLevelList(EvaluatedLevelQuery evaluatedLevelQuery) {
        return evaluatedLevelMapper.selectEvaluatedLevelByCondition(evaluatedLevelQuery);
    }

    @Override
    public int evaluate(int levelValue, String terminalCode, int employeeId) {
        EvaluatedPointQuery evaluatedPointQuery = new EvaluatedPointQuery();
        evaluatedPointQuery.setTerminalCode(terminalCode);
        List<EvaluatedPoint> evaluatedPoints = evaluatedPointMapper.selectByCondition(evaluatedPointQuery);
        if (evaluatedPoints.size() == 1) {
            EvaluatedLevelQuery evaluatedLevelQuery = new EvaluatedLevelQuery();
            evaluatedLevelQuery.setValue(levelValue);
            List<EvaluatedLevel> evaluatedLevels = evaluatedLevelMapper.selectEvaluatedLevelByCondition(evaluatedLevelQuery);
            if (evaluatedLevels.size() == 1) {
                EvaluatedRecord evaluatedRecord = new EvaluatedRecord();
                evaluatedRecord.setLevelId(evaluatedLevels.get(0).getId());
                evaluatedRecord.setPointId(evaluatedPoints.get(0).getId());
                evaluatedRecord.setEmployeeId((long)employeeId);
                return evaluatedRecordMapper.insertSelective(evaluatedRecord);
            }
        }
        return 0;
    }

    @Override
    public Object queryCurrentEmployeeInfo(String phone) {
        /*EvaluatedPointQuery evaluatedPointQuery = new EvaluatedPointQuery();
        evaluatedPointQuery.setTerminalCode(terminalCode.trim());
        List<EvaluatedPoint> evaluatedPoints = evaluatedPointMapper.selectByCondition(evaluatedPointQuery);
        if (evaluatedPoints.size() == 1) {*/
        Employee employee = employeeMapper.queryEmployeeByPhone(phone).get(0);
        Map map = new HashMap<>();
        map.put("id", employee.getId());
        map.put("name", employee.getName());
        map.put("headPicUrl", employee.getHeadPicUrl());
        return map;
//        }
//        return null;
    }

    @Override
    public Object queryEvaluatePoints(int compoundsId) {
        List<Map> results = new ArrayList<>();
        EvaluatedPointQuery evaluatedPointQuery = new EvaluatedPointQuery();
        evaluatedPointQuery.setCompoundsId((long) compoundsId);
        List<EvaluatedPoint> evaluatedPoints = evaluatedPointMapper.selectByCondition(evaluatedPointQuery);
        for (int i = 0; i < evaluatedPoints.size(); i++) {
            Map<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("id", evaluatedPoints.get(i).getId());
            resultMap.put("name", evaluatedPoints.get(i).getName());
            results.add(resultMap);
        }
        return results;
    }

    @Override
    public Object queryTotalEvaluationsListByEvaluatePoint(int evaluatePointId, String queryTime) throws ParseException {
        Map<String, Object> result = new LinkedHashMap<>();
        List<Map> details = new ArrayList<>();



        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = dateFormat.parse(queryTime);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        String day_first = df.format(calendar.getTime());
        StringBuffer str = new StringBuffer().append(day_first).append(
                " 00:00:00");
        day_first = str.toString();
        // System.out.println(day_first);

        calendar.add(Calendar.MONTH, 1); // 加一个月
        calendar.set(Calendar.DATE, 1); // 设置为该月第一天
        calendar.add(Calendar.DATE, -1); // 再减一天即为上个月最后一天

        String day_last = df.format(calendar.getTime());
        StringBuffer endStr = new StringBuffer().append(day_last).append(
                " 23:59:59");
        day_last = endStr.toString();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date endTime = format.parse(day_last);
        Date startTime = format.parse(day_first);
        List<EvaluatedRecord> evaluatedRecords = evaluatedRecordMapper.queryTotalEvaluationsListByEvaluatePoint(evaluatePointId, startTime, endTime);

        int totalPerfectNum = 0;
        int totalGreatNum = 0;
        int totalGoodNum = 0;

        for(EvaluatedRecord evaluatedRecord : evaluatedRecords) {
            Map<String, Object> detail = new ConcurrentHashMap<>();

            int levelValue = evaluatedLevelMapper.selectByPrimaryKey(evaluatedRecord.getLevelId()).getValue();
            switch (levelValue) {
                case 1 :
                     totalGoodNum += 1;
                    detail.put("evaluattion", "不满意");
                    detail.put("evaluattionBgColor", "#2fd0021b");
                    break;
                case 2:
                    totalGreatNum += 1;
                    detail.put("evaluattion", "一般");
                    detail.put("evaluattionBgColor", "#2ffbdd00");
                    break;
                case 3:
                    totalPerfectNum += 1;
                    detail.put("evaluattion", "满意");
                    detail.put("evaluattionBgColor", "#2f7ed321");
                    break;
                default:

            }
            detail.put("employeeName", employeeMapper.queryEmployeeById(evaluatedRecord.getEmployeeId().intValue()).getName());
            detail.put("timeOfevaluation", evaluatedRecord.getEvaluatedTime());
            details.add(detail);

        }
        result.put("totalPerfectNum", totalPerfectNum);
        result.put("totalGreatNum", totalGreatNum);
        result.put("totalGoodNum", totalGoodNum);

//        result.put("totalPerfectNumColor", "#2f7ed321");
//        result.put("totalGreatNumColor", "#2ffbdd00");
//        result.put("totalGoodNumColor", "#2fd0021b");

        result.put("details", details);
        return result;
    }

}
